<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@page import="com.liuya.safe.policy.handler.AbstractPolicyDesignHandler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
    I18N i18n = I18N.getInstance();
    AbstractPolicyDesignHandler handler = (AbstractPolicyDesignHandler) request.getAttribute("handler");
%>

<html>
<%@ include file="../common/taglib.jsp"%>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="shortcut icon" href="/style/def/favicon.ico">
    <%@ include file="../common/static.jsp"%>

    <link type="text/css" rel="stylesheet" href="${staticPath}/def/css/syntaxhighlighter/shCoreDefault.css"/>

    <script type="text/javascript" src="${staticPath}/def/js/syntaxhighlighter/shCore.js"></script>
    <script type="text/javascript" src="${staticPath}/def/js/syntaxhighlighter/shBrushCss.js"></script>
    <script type="text/javascript" src="${staticPath}/def/js/syntaxhighlighter/shBrushJScript.js"></script>
    <script type="text/javascript" src="${staticPath}/def/js/syntaxhighlighter/shBrushJava.js"></script>

    <title><%=i18n.say("Test_Policy")%> -- <%=handler.getPolicy().getName() %>
    </title>
</head>
<body>
<jsp:include page="../common/header.jsp"></jsp:include>
<%
    boolean testUserNeeded = handler.isTestUserNeeded();
    String[] bdFields = handler.getTestBusinessDataFields();
    String[] ctxFields = handler.getTestContextFields();
    String script = handler.getScript();
    String id = request.getParameter("id");

    String oper = request.getParameter("oper");

    String[] mainMenuHrefs = new String[]{
            "returnMain.do?oper=return&id=" + id
    };
    String mngText = i18n.say("User_category");
    if ("businessData".equals(handler.getPolicyType())) {
        mngText = i18n.say("Business_data");
    }
    String[] mainMenuTexts = new String[]{
            mngText
    };
    String edit = i18n.say("Continue_edit");
    if ("loadFresh".equals(oper)) {
        edit = i18n.say("Edit");
    }
    String editUrl = "policyDesign.do?id=" + id;
    if (handler.getPolicy().isRawScript()) {
        editUrl = "policyRaw.do?id=" + id;
    }
    String[] subMenuHrefs = new String[]{
            "javascript:savePolicy();",
            editUrl
    };
    String[] subMenuTexts = new String[]{
            i18n.say("Save"),
            edit
    };

    request.setAttribute("mainMenuHrefs", mainMenuHrefs);
    request.setAttribute("mainMenuTexts", mainMenuTexts);
    request.setAttribute("subMenuHrefs", subMenuHrefs);
    request.setAttribute("subMenuTexts", subMenuTexts);
%>

<%@include file="../common/menu.jsp" %>

<div class="smallContainer">
    <label><%=i18n.say("SQLScript")%>
    </label>
    <div id="script">
        <pre class="brush: java;"><%=script%></pre>
    </div>

    <form id="testForm" method="post" action="runTest.do?oper=runTest&id=<%=id%>">
        <%
            if (testUserNeeded) {
        %>
        <jsp:include page="../common/inputTestUser.jsp"></jsp:include>
        <br/>
        <% } %>

        <%
            if (ctxFields.length > 0) {
                request.setAttribute("testContextFields", ctxFields);
        %>
        <jsp:include page="../common/inputTestContext.jsp"></jsp:include>
        <br/>
        <% } %>

        <div id="bdDiv">
            <%
                if (bdFields.length > 0) {
                    request.setAttribute("testBusinessDataFields", handler.getTestBusinessDataFields());
                    request.setAttribute("testBusinessDataFieldTypes", handler.getTestBusinessDataFieldTypes());
            %>
            <jsp:include page="../common/inputTestBusinessData.jsp"></jsp:include>
            <% } %>
        </div>

        <p>
            <input type="submit" value="<%=i18n.say( "Run" )%>"/>
        </p>
    </form>

    <div id="result">
    </div>

    <div id="output2">
    </div>

</div>

<%@include file="../common/footer.jsp" %>

<script language="javascript">
    jQuery(document).ready(function () {
        SyntaxHighlighter.all();

        var options = {
            beforeSubmit: checkTestForm,
            success: showResponse
        };
        jQuery('#testForm').submit(function () {
            jQuery(this).ajaxSubmit(options);
            return false;
        });

        // validate
        jQuery('#testForm').validate();
    });

    function checkTestForm() {
        return jQuery("#testForm").valid();
    }

    function showResponse(responseText, statusText, xhr, $form) {
        jQuery("#result").html(responseText);
    }

    function loadJavabeanProperty() {
        var bdClass = jQuery("#testForm :input[name=bdClass]").attr("value");

        jQuery.ajax({
            url: 'setBusinessDataClass.do?oper=setBusinessDataClass',
            type: 'POST',
            data: {"id": "<%=id%>", "businessDataClass": bdClass},
            error: function () {
                alert('Error business data class');
            },
            success: function (resp) {
                jQuery("#bdDiv").html(resp);
            }
        });
    }

    function savePolicy() {
        jQuery.ajax({
            url: 'save.do?oper=save',
            type: 'POST',
            data: {"id": "<%=id%>"},
            error: function () {
                alert('Error save policy');
            },
            success: function (xml) {
                alert("<%=i18n.say("saved")%>");
            }
        });
    }

    function setDataPicker(name) {
        var bdClass = jQuery("#testForm :input[name=" + name + "]").datepicker({dateFormat: 'yy-mm-dd'});
    }
</script>
</body>
</html>